﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;




namespace EmbeddedResources1
{
    public static class Languages
    {

        public enum LanguagesTypes
        {
            frFR ,
            enGB,
            esES,
            ptPT,
            ptBR,
            DEFAULT,
        }

        public static LanguagesTypes GetLanguage(string lang)
        {
            if (lang == "fr-FR")
                return LanguagesTypes.frFR;
            else if (lang == "en-GB")
                return LanguagesTypes.enGB;
            else if (lang == "pt-PT")
                return LanguagesTypes.ptPT;
            else if (lang == "es-ES")
                return LanguagesTypes.esES;
            else if (lang == "pt-BR")
                return LanguagesTypes.ptBR;

            return LanguagesTypes.DEFAULT;
        }

        public static IList<string> GetAllLanguageStrings()
        {
            IList<String> list = new List<String>();
            foreach (LanguagesTypes type in Enum.GetValues(typeof(LanguagesTypes)))
            {
                list.Add(GetLanguageCultureInfoString(type));
            }
            return list;
        }

        public static string GetLanguageCultureInfoString(LanguagesTypes type)
        {
            switch (type)
            {
                case LanguagesTypes.frFR:
                    return "fr-FR";
                    break;
                case LanguagesTypes.enGB:
                    return "en-GB";
                    break;
                case LanguagesTypes.esES:
                    return "es-ES";
                    break;
                case LanguagesTypes.ptPT:
                    return "pt-PT";
                    break;
                case LanguagesTypes.ptBR:
                    return "pt-BR";
                    break;
                case LanguagesTypes.DEFAULT:
                    return "DEFAULT";
                    break;
                default:
                    return "UNKNOWN";
                    break;
            }
        }
    }

    public class LocalizationProject
    {
        public IList<string> Assemblies = new List<string>();
        public string projectName;
        public IList<ResourceKeyValueItem> AllResources = new List<ResourceKeyValueItem>();

        public ResourceKeyValueItem FindResourceItem(string assembly, string resourceName)
        {
            return AllResources.Where(res => res.AssemblyName == assembly 
                                      && res.ResourceName == resourceName 
                                      && res.Language == Languages.LanguagesTypes.DEFAULT).Single<ResourceKeyValueItem>();
        }

        public IList<ResourceKeyValueItem> GetAllResourcesByLanguage(string assembly,string resourceName, Languages.LanguagesTypes language)
        {
            return AllResources.Where(res => res.AssemblyName == assembly 
                                        && res.ResourceName == resourceName).ToList<ResourceKeyValueItem>();
        }

        public void ExportLanguageToCSV(Languages.LanguagesTypes language)
        {
            DirectoryInfo dir = null;
            string languageStr = Languages.GetLanguageCultureInfoString(language);
            if (!(Directory.Exists(".\\Output\\" + languageStr)))
            {
                dir = Directory.CreateDirectory(".\\Output\\" + languageStr);
            }
            else
            {
                dir = new DirectoryInfo(".\\Output\\" + languageStr);
            }

            string outputCSVFile = dir.FullName + "\\" + language + ".csv";

            string str = "";

            foreach (ResourceKeyValueItem res in AllResources)
            {
                if (res.Language == language)
                    str += res.ExportToCSV();
            }
            //Output to the CSV
            File.WriteAllText(outputCSVFile, str,Encoding.UTF8);
        }

        public void ExportLanguage(Languages.LanguagesTypes language)
        {
            DirectoryInfo dir = null;
            string languageStr = Languages.GetLanguageCultureInfoString(language);
            if (!(Directory.Exists(".\\Output\\" + languageStr)))
            {
                dir = Directory.CreateDirectory(".\\Output\\" + languageStr);
            }
            else
            {
                dir = new DirectoryInfo(".\\Output\\" + languageStr);
            }


            foreach (string assembly in this.Assemblies)
            {
                IList<string> resourceFile = new List<string>();
                IList<ResourceKeyValueItem> listByAssembly = this.AllResources.Where(res => res.Language == language && res.AssemblyName == assembly).ToList<ResourceKeyValueItem>();

                var group = from r in listByAssembly
                            group r by r.ResourceName into g
                            select new { ResourceName = g.Key, Resources = g.ToList<ResourceKeyValueItem>() };

                foreach (var resourceGroup in group)
                {
                    if (resourceGroup.Resources.Count > 0)
                    {
                        int ind = resourceGroup.ResourceName.IndexOf(".resources");
                        string dup = (string)resourceGroup.ResourceName.Clone();
                        dup = dup.Insert(ind, "." + languageStr);
                        string outputResourceFile = dir.FullName + "\\" + dup;

                        //Executar o resgen
                        System.Resources.ResourceWriter resWriter = new System.Resources.ResourceWriter(outputResourceFile);

                        foreach (ResourceKeyValueItem resource in resourceGroup.Resources)
                        {
                            if (resource != null && resource.Value != null && resource.Value != string.Empty)
                                resWriter.AddResource(resource.Key, resource.TranslatedValue);
                        }

                        resWriter.Generate();
                        resWriter.Close();
                        resourceFile.Add(new FileInfo(outputResourceFile).Name);
                    }
                }
                //executar o AL
                string s = "/t:lib /culture:\"" + languageStr + "\"";
                foreach (string resString in resourceFile)
                {
                    string tmp = " /embed:" + resString;
                    s += tmp;
                }

                string tmp2 = " /out:" + assembly + ".resources.dll\r\n";
                s += tmp2;


                if (File.Exists(Path.Combine(dir.FullName, "build.bat")))
                    File.Delete(Path.Combine(dir.FullName, "build.bat"));
                
                File.WriteAllText(Path.Combine(dir.FullName,"build.bat"), "..\\..\\al.exe " + s);

                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                //proc.EnableRaisingEvents = false;
                proc.StartInfo.FileName = "build.bat";
                proc.StartInfo.WorkingDirectory = dir.FullName;
                proc.Start();

                proc.WaitForExit();
                File.Delete(Path.Combine(dir.FullName, "build.bat"));
            }
            /*
            foreach (string assembly in this.Assemblies)
            {
                IList<string> resourceFile = new List<string>();
                foreach (ResourceItem resourceItem in this.GetAllResourcesByLanguage)
                {
                    IList<ResourceValueItem> list = this.GetAllResourcesByLanguage(resourceItem,language);
                    if (list.Count > 0)
                    {
                        int ind = resourceItem.ResourceName.IndexOf(".resources");
                        string dup = (string)resourceItem.ResourceName.Clone();
                        dup = dup.Insert(ind, "." + language);
                        string outputResourceFile = dir.FullName + "\\" + dup;

                        //Executar o resgen
                        System.Resources.ResourceWriter resWriter = new System.Resources.ResourceWriter(outputResourceFile);

                        foreach (ResourceValueItem resource in list)
                        {
                            if (resource != null && resource.Value != null && resource.Value != string.Empty)
                                resWriter.AddResource(resource.ResourceKeyText, resource.Value);
                        }

                        resWriter.Generate();
                        resWriter.Close();
                        resourceFile.Add(new FileInfo(outputResourceFile).Name);
                    }
                }
                //executar o AL
                string s = "/t:lib /culture:\"" + language + "\"";
                foreach (string resString in resourceFile)
                {
                    string tmp = " /embed:" + resString;
                    s += tmp;
                }

                string tmp2 = " /out:" + item.Name + ".resources.dll\r\n";
                s += tmp2;

                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                //proc.EnableRaisingEvents = false;
                proc.StartInfo.FileName = "c:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\al.exe";
                proc.StartInfo.Arguments = s;
                proc.StartInfo.WorkingDirectory = dir.FullName;
                proc.Start();
            } 
            */
        }

//        public void ExportLanguage()
//        {
            //DirectoryInfo dir = null;
            //string language = Languages.GetLanguageCultureInfoString((Languages.LanguagesTypes)comboBox1.SelectedItem);
            //if (!(Directory.Exists(".\\Output\\" + language)))
            //{
            //    dir = Directory.CreateDirectory(".\\Output\\" + language);
            //}
            //else
            //{
            //    dir = new DirectoryInfo(".\\Output\\" + language);
            //}
            //MainForm main = this.Owner as MainForm;
            //if (main != null)
            //{ 
            //    foreach (AssemblyItem item in main.Project.Assemblies)
            //    {
            //        IList<string> resourceFile = new List<string>();
            //        foreach (ResourceItem resourceItem in item.Resources)
            //        {
            //            IList<ResourceValueItem> list = main.Project.GetAllResourcesByLanguage(resourceItem, (Languages.LanguagesTypes)comboBox1.SelectedItem);
            //            if (list.Count > 0)
            //            {
            //                int ind = resourceItem.ResourceName.IndexOf(".resources");
            //                string dup = (string)resourceItem.ResourceName.Clone();
            //                dup = dup.Insert(ind, "." + language);
            //                string outputResourceFile = dir.FullName + "\\" + dup;

            //                //Executar o resgen
            //                System.Resources.ResourceWriter resWriter = new System.Resources.ResourceWriter(outputResourceFile);

            //                foreach (ResourceValueItem resource in list)
            //                {
            //                    if (resource != null && resource.Value != null && resource.Value != string.Empty)
            //                        resWriter.AddResource(resource.ResourceKeyText, resource.Value);
            //                }

            //                resWriter.Generate();
            //                resWriter.Close();
            //                resourceFile.Add(new FileInfo( outputResourceFile).Name);
            //            }
            //        }
            //        //executar o AL
            //        string s = "/t:lib /culture:\"" + language + "\"";
            //        foreach(string resString in resourceFile)
            //        {
            //            string tmp = " /embed:" + resString;
            //            s += tmp;
            //        }

            //        string tmp2 = " /out:" + item.Name + ".resources.dll\r\n";
            //        s += tmp2;

            //        System.Diagnostics.Process proc = new System.Diagnostics.Process();
            //        //proc.EnableRaisingEvents = false;
            //        proc.StartInfo.FileName =  "c:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\al.exe";
            //        proc.StartInfo.Arguments = s;
            //        proc.StartInfo.WorkingDirectory = dir.FullName;
            //        proc.Start();
            //    } 
            //}
            //this.Close();

        public bool IsLanguageDefined(Languages.LanguagesTypes lang)
        {
            return this.AllResources.Where(res => res.Language == lang).Count() > 0;
        }

        public void RemoveResourcesByAssembly(string assembly)
        {
            for (int i = 0; i < AllResources.Count; i++)
            {
                ResourceKeyValueItem item = AllResources[i];
                if (item.AssemblyName == assembly)
                {
                    AllResources.RemoveAt(i);
                    i--;
                }
            }
        }
    }
//    }
}
